package 多线程进阶;

import java.util.concurrent.Semaphore;

/**
 * Semaphore的使用示例
 */
public class TestDemo4 {
    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(4);
        for (int i = 0; i < 20; i++) {
            Thread thread=new Thread(()->{
                try {
                    System.out.println("P操作:申请资源");
                    semaphore.acquire();
                    System.out.println("获取资源成功");
                    Thread.sleep(2000);
                    semaphore.release();
                    System.out.println("V操作:释放资源");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }
}
